package com.beamfield.controller.manage.sys;

import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.modelVo.DocumentVo;
import com.beamfield.modelVo.TreeVo;
import com.beamfield.service.manage.sys.DocumentService;

/**
 * 文档
 */
@RequestMapping("/cms/document")
@Controller
public class DocumentController extends BaseController{

    private final DocumentService documentService;

    @Autowired
    public DocumentController(DocumentService documentService) {
        this.documentService = documentService;
    }

    @RequestMapping(value = "/document")
    public ModelAndView document(){
        return new ModelAndView();
    }

    @RequestMapping(value = "/full_search")
    public ModelAndView fullSearch(){
        return new ModelAndView();
    }

    @RequestMapping(value = "/fileType")
    public ModelAndView fileType(){
        return new ModelAndView();
    }
    /**
     * 查询框输入提示
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/rapidTips", method = RequestMethod.GET)
    public ApiResult rapidTips(String query, HttpServletRequest request){
        if(StringUtils.isEmpty(query)){
            return new ApiResult();
        }
        Long beamId = super.getBeamId(request);
        List<TreeVo> documentNameByQuery = documentService.getDocumentNameByQuery(query, beamId);
        return new ApiResult(documentNameByQuery);
    }

    /**
     * 获取文档
     * @param request
     * @return
     */
    @RequestMapping(value = "/readDocumentData", method = RequestMethod.GET)
    @ResponseBody
    public PageResult readDocumentData(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String type = request.getParameter("type");
        String queryWords = request.getParameter("queryWords");
        String fileType = request.getParameter("fileType");
        
        // 根据年份搜索
        String years = request.getParameter("years");

        if(StringUtils.isEmpty(type)){
            return null;
        }
        if(Objects.equals("1", type)){
            List<DocumentVo> documentVoList = documentService.getDocument(offset, limit, beamId, fileType, queryWords, years);
            long total = documentService.countDocument(beamId, fileType, queryWords, years);
            return new PageResult(total, CommonUtils.parseLong(offset, 0L), documentVoList);
        }else{
            return documentService.getDocumentFromIndex(offset, limit, fileType, queryWords, beamId);
        }
    }

    /**
     * 删除文档
     * @param request
     * @return
     */
    @RequestMapping(value = "/delDocument", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult delDocument(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String id = request.getParameter("id");
        if(StringUtils.isEmpty(id)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        int result = 0;
        try{
            result = documentService.delDocument(beamId, id);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "删除成功");
        }else {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 保存文档
     * @param request
     * @return
     */
    @RequestMapping(value = "/doSaveDocument", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doSaveDocument(HttpServletRequest request, @RequestParam("file") MultipartFile multipartFile){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        String typeId = request.getParameter("typeId");
        String years = request.getParameter("years");
        String id = request.getParameter("id");
        if(StringUtils.isEmpty(typeId) || StringUtils.isEmpty(years) || multipartFile == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        int result = 0;
        try {
            long fileId = super.saveFile((CommonsMultipartFile) multipartFile);
            result = documentService.saveDocument(beamId, userId, fileId, typeId, id, years);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "操作成功");
        }else {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 新增分类
     * @param request
     * @return
     */
    @RequestMapping(value = "/doSaveFileType", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doSaveFileType(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        String pid = request.getParameter("pid");
        String typeName = request.getParameter("typeName");
        String id = request.getParameter("id");
        int result = 0;
        try {
            result = documentService.doSaveFileType(beamId, userId, pid, typeName, id);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "操作成功");
        }else {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 删除分类
     * @param request
     * @return
     */
    @RequestMapping(value = "/delFileType", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult delFileType(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String id = request.getParameter("id");
        if(StringUtils.isEmpty(id)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        int result = 0;
        try{

            List<DocumentVo> documentVoList = documentService.getDocument("0", "1", beamId, id, null, null);
            if (documentVoList.size()>0){
                return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),"此分类或者子分类下还有文件，不能删除");
            }

            result = documentService.delFileType(beamId, id);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result >= 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "删除成功");
        }else {
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * 获取文件分类树
     * @param request
     * @return
     */
    @RequestMapping(value = "/readFileTypeTree", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult readFileTypeTree(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        List<TreeVo> fileTypeTree = documentService.getFileTypeTree(beamId);
        return new ApiResult(fileTypeTree);
    }
}
